<template>
  <div class="app-container">
    <div class="search-container" style="display: flex">
      <el-form-item label="省份" style="width: 200px">
        <el-select
          @change="provinceChange"
          v-model="params.provinceId"
          placeholder="请选择省份"
        >
          <el-option
            v-for="v in provinceList"
            :key="v.provinceId"
            :label="v.pname"
            :value="v.provinceId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="城市" style="width: 200px; margin-left: 10px">
        <el-select v-model="params.cityId" placeholder="请选择市">
          <el-option
            v-for="v in cityList"
            :key="v.cityId"
            :label="v.cityName"
            :value="v.cityId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="确认日期" style="width: 200px; margin-left: 10px">
        <el-date-picker
          v-model="params.verDatetime"
          type="date"
          placeholder="请选择"
        />
      </el-form-item>
      <el-button type="primary" style="margin-left: 10px" @click="clearParams"
        >清空</el-button
      >
      <el-button type="primary" style="margin-left: 10px" @click="handleSearch"
        >查询</el-button
      >
    </div>

    <el-card shadow="never" class="table-container">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="verifyId" width="80" label="编号" />
        <el-table-column prop="pname" width="100" label="省份" />
        <el-table-column prop="cityName" width="100" label="城市" />
        <el-table-column label="AQI等级">
          <template #default="scope">
            <!-- <span v-if="scope.row.aqiStr == '一'" class="aqi-level-1-style"
              >优（一）</span
            >
            <span v-if="scope.row.aqiStr == '二'" class="aqi-level-2-style"
              >良（二）</span
            >
            <span v-if="scope.row.aqiStr == '三'" class="aqi-level-3-style"
              >轻度污染（三）</span
            >
            <span v-if="scope.row.aqiStr == '四'" class="aqi-level-4-style"
              >中度污染（四）</span
            >
            <span v-if="scope.row.aqiStr == '五'" class="aqi-level-5-style"
              >重度污染（五）</span
            >
            <span v-if="scope.row.aqiStr == '六'" class="aqi-level-6-style"
              >严重污染（六）</span
            > -->
            <el-button type="success" plain v-if="scope.row.aqiStr == '一'"
              >优（一）</el-button
            >
            <el-button
              type="success"
              color="#baae00"
              plain
              v-if="scope.row.aqiStr == '二'"
              >良（二）</el-button
            >
            <el-button
              type="success"
              color="#ffb000"
              plain
              v-if="scope.row.aqiStr == '三'"
              >轻度污染（三）</el-button
            >
            <el-button
              type="success"
              color="#ff4600"
              plain
              v-if="scope.row.aqiStr == '四'"
              >中度污染（四）</el-button
            >
            <el-button
              type="success"
              color="#006bff"
              plain
              v-if="scope.row.aqiStr == '五'"
              >重度污染（五）</el-button
            >
            <el-button
              type="success"
              color="#b000ff"
              plain
              v-if="scope.row.aqiStr == '六'"
              >严重污染（六）</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="gname" width="100" label="网格员" />
        <el-table-column prop="sname" width="100" label="反馈者" />
        <el-table-column label="照片">
          <template #default="scope">
            <el-image
              style="
                width: 100px;
                height: 100px;
                background-color: #fff;
                z-index: 9999;
              "
              :src="
                baseUrl + '/admin-api/file/imgId/' + scope.row.fileUploadImgId
              "
            />
          </template>
        </el-table-column>
        <el-table-column sortable prop="verDatetime" width="120" label="日期">
          <template #default="scope">
            {{ formatDate(new Date(scope.row.verDatetime)) }}
          </template>
        </el-table-column>
        <el-table-column width="100" label="时间">
          <template #default="scope">
            {{ formatTime(new Date(scope.row.verDatetime)) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="verify(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="margin-top: 20px"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageRes.totalRow"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
    </el-card>
    <el-dialog v-model="verifyDialogFormVisible" title="查看详情" width="800">
      <el-descriptions title="" :column="1" border>
        <el-descriptions-item label="确认AQI数据编号">
          {{ curRow.verifyId }}
        </el-descriptions-item>
        <el-descriptions-item label="确认信息所在地址">
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.pname
          }}</el-tag>
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.cityName
          }}</el-tag>
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.faddress
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 1">
          <el-tag size="small" style="margin-right: 10px">一级</el-tag>
          <el-tag size="small">优</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 2">
          <el-tag size="small" style="margin-right: 10px">二级</el-tag>
          <el-tag size="small">良</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 3">
          <el-tag size="small" style="margin-right: 10px">三级</el-tag>
          <el-tag size="small">轻度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 4">
          <el-tag size="small" style="margin-right: 10px">四级</el-tag>
          <el-tag size="small">中度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 5">
          <el-tag size="small" style="margin-right: 10px">五级</el-tag>
          <el-tag size="small">重度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="预测AQI等级" v-if="curRow.aqiPreId == 6">
          <el-tag size="small" style="margin-right: 10px">六级</el-tag>
          <el-tag size="small">严重污染</el-tag>
        </el-descriptions-item>
        <!-- 分割线 -->
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '一'">
          <el-tag size="small" style="margin-right: 10px">一级</el-tag>
          <el-tag size="small">优</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '二'">
          <el-tag size="small" style="margin-right: 10px">二级</el-tag>
          <el-tag size="small">良</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '三'">
          <el-tag size="small" style="margin-right: 10px">三级</el-tag>
          <el-tag size="small">轻度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '四'">
          <el-tag size="small" style="margin-right: 10px">四级</el-tag>
          <el-tag size="small">中度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '五'">
          <el-tag size="small" style="margin-right: 10px">五级</el-tag>
          <el-tag size="small">重度污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认AQI等级" v-if="curRow.aqiStr == '六'">
          <el-tag size="small" style="margin-right: 10px">六级</el-tag>
          <el-tag size="small">严重污染</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="反馈日期时问">
          <el-tag size="small" style="margin-right: 10px">{{
            formatDate(new Date(curRow.fdatetime))
          }}</el-tag>
          <el-tag size="small">{{
            formatTime(new Date(curRow.fdatetime))
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="确认日期时问">
          <el-tag size="small" style="margin-right: 10px">{{
            formatDate(new Date(curRow.verDatetime))
          }}</el-tag>
          <el-tag size="small">{{
            formatTime(new Date(curRow.verDatetime))
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="网格员信息">
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.gname
          }}</el-tag>
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.gphone
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="公众监督员信息">
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.sname
          }}</el-tag>
          <el-tag size="small" style="margin-right: 10px">{{
            curRow.sphone
          }}</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="公众监督员反馈信息描述">
          {{ curRow.finfo }}
        </el-descriptions-item>
        <el-descriptions-item label="图片">
          <el-image
            style="
              width: 100px;
              height: 100px;
              background-color: #fff;
              z-index: 9999;
            "
            :src="baseUrl + '/admin-api/file/imgId/' + curRow.fileUploadImgId"
            :preview-src-list="[
              baseUrl + '/admin-api/file/imgId/' + curRow.fileUploadImgId,
            ]"
          />
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="verifyDialogFormVisible = false"
            >返回</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import VerifyAPI from "@/api/verify";
import DistrictAPI from "@/api/district";
import { ElMessage } from "element-plus";

const params: any = ref({
  provinceId: null,
  cityId: null,
  verDatetime: null,
  pageNum: 1,
  pageSize: 4,
});

const baseUrl = import.meta.env.VITE_APP_BASE_API;

const provinceList: any = ref([]);

const cityList: any = ref([]);

const verifyDialogFormVisible = ref(false);

const curRow: any = ref({});

const pageRes = ref({
  totalPage: 0,
  totalRow: 0,
});

const tableData: any = ref([]);

function reqTableData() {
  VerifyAPI.getVerifyList(params.value).then((res) => {
    console.log(res);
    tableData.value = [];
    pageRes.value.totalPage = res.totalPage;
    pageRes.value.totalRow = res.total;
    if (res.list == null) return;
    for (const v of res.list) {
      tableData.value.push(v);
    }
  });
}

function pageChange(v: any) {
  console.log(v);
  console.log();
  params.value.pageNum = v;
  reqTableData();
}

function pad(num: any) {
  return num.toString().padStart(2, "0");
}

function formatDateTime(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`;
}

function formatDate(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${pad(month)}-${pad(day)}`;
}

function formatTime(date: any) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${pad(hour)}:${pad(minute)}:${pad(second)}`;
}

function verify(row: any) {
  curRow.value = row;
  console.log(curRow.value);
  verifyDialogFormVisible.value = true;
}

onMounted(() => {
  reqTableData();
  reqProvinceList();
});

function reqProvinceList() {
  DistrictAPI.provinceList().then((res) => {
    for (const v of res) {
      provinceList.value.push(v);
    }
  });
}

function reqCityList() {
  DistrictAPI.cityList(params.value.provinceId).then((res) => {
    for (const v of res) {
      cityList.value.push(v);
    }
  });
}

function provinceChange(v: any) {
  console.log(v);
  console.log(params.value.provinceId);
  cityList.value = [];
  params.value.cityId = "";
  reqCityList();
}

function clearParams() {
  params.value = {
    provinceId: null,
    cityId: null,
    verDatetime: null,
    pageNum: 1,
    pageSize: 4,
  };
}

function handleSearch() {
  reqTableData();
}
</script>
